package com.predict.javademo.services.impl;

import com.predict.javademo.services.AutoEvaService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Service
@Slf4j
@RequiredArgsConstructor
public class AutoEvaServiceImpl implements AutoEvaService {

    @Value("${flask.url.auto_eva}")
    private String autoEvaUrl;
    
    private final RestTemplate restTemplate;
    
    @Override
    public ResponseEntity<?> handleAutoEva(Map<String, Object> requestBody) {
        try {
            log.info("发送自适应评估请求到: {}, 参数: {}", autoEvaUrl, requestBody);
            
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            
            HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
            
            ResponseEntity<String> response = restTemplate.postForEntity(autoEvaUrl, requestEntity, String.class);
            
            log.info("自适应评估请求完成，状态码: {}", response.getStatusCodeValue());
            return response;
        } catch (RestClientException e) {
            log.error("自适应评估请求失败: {}", e.getMessage(), e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("自适应评估请求处理失败: " + e.getMessage());
        }
    }
}